<template>
  <page-header-wrapper :title="false">
    <a-card title="订单详情">
      <a-descriptions bordered title="" size="small">
        <a-descriptions-item label="订单" :span="3"> {{ info?.id }} </a-descriptions-item>
        <a-descriptions-item label="创建人" :span="3"> {{ info?.creatorName }} </a-descriptions-item>
        <a-descriptions-item label="店面" :span="3"> {{ info?.storeName }} </a-descriptions-item>
        <a-descriptions-item label="收支类型" :span="3"> {{ render(info?.payType) }} </a-descriptions-item>
        <a-descriptions-item label="日期" :span="3"> {{ info?.orderDate }} </a-descriptions-item>
        <a-descriptions-item label="金额" :span="3"> {{ info?.amount }} </a-descriptions-item>
        <a-descriptions-item label="状态" :span="3">
          {{ info?.status == '1' ? '已支付' : '未支付' }}
        </a-descriptions-item>
        <a-descriptions-item label="审核状态" :span="3">
          {{ info?.checkStatus === '1' ? '已审核' : '未审核' }}
        </a-descriptions-item>
        <a-descriptions-item label="审核人" :span="3" v-if="info?.checkStatus == 1">
          {{ info.checkerName }}
        </a-descriptions-item>
        <a-descriptions-item label="备注" :span="3"> {{ info?.memo }} </a-descriptions-item>
        <a-descriptions-item label="会员姓名" :span="3"> {{ info?.memName }} </a-descriptions-item>
        <a-descriptions-item label="会员电话" :span="3"> {{ info?.memPhone }} </a-descriptions-item>
        <a-descriptions-item label="会员卡号" :span="3"> {{ info?.cardNo }} </a-descriptions-item>
        <a-descriptions-item label="会员积分" :span="3"> {{ info?.bigintegral }} </a-descriptions-item>
        <a-descriptions-item label="操作" :span="3">
          <div style="display: flex">
            <a-button type="danger" style="margin-right: 5px" v-if="info?.checkStatus === '0'" @click="handleBack(info)" >撤销订单</a-button
            >
            <a-button type="primary" style="margin: 0px 5px" v-if="info?.checkStatus === '0'" @click="handleCheckPur()">审核订单</a-button
            >
            <a-button
              type="success"
              v-if="info?.checkStatus === '1'"
              style="margin: 0px 5px"
              @click="handleUnCheckPur()"
            >反审核</a-button
            >
            <a-button
              type="primary"
              style="margin: 0px 5px; background: #ffb752; border-color: #ffb752"
              @click="handleOrder(info)"
            >继续新增</a-button
            >
            <!-- <a-button style="margin: 0px 5px">打印退库单(汇总)</a-button>
            <a-button style="margin: 0px 5px">打印退库单(汇总)</a-button> -->
          </div>
        </a-descriptions-item>
      </a-descriptions>
    </a-card>
    <a-card title="付款信息" style="margin-top: 24px" v-if="this.info?.status == '1'">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="8">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.keyword" placeholder="请输入筛选" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="handleSearch(queryParam)" :loading="searchLoading">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
                <!-- <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :loading="searchLoading"
        :data-source="loadData"
        :pagination="pagination"
        @change="handleTableChange"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
      </a-table>
    </a-card>
    <a-card title="操作记录" style="margin-top: 24px">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="8">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.keyword" placeholder="请输入筛选" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="handleRecordSearch(queryParam)" :loading="recordLoading">查询</a-button
                >
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
                <!-- <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table
        ref="recordtable"
        size="default"
        rowKey="key"
        :columns="recordcolumns"
        :loading="recordLoading"
        :data-source="recordloadData"
        :pagination="recordPagination"
        @change="handleRecordTableChange"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="option" slot-scope="text, record">
          <template> {{ record.empName }} <a-tag color="#428bca" @click="handleUserDetail(record)">详情</a-tag> </template>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { getOperRecordListUsingPOST } from '@/api/api/caigouguanli.ts'
import {
  getAccRecordUsingGET,
  getSalPayUsingGET,
  revokeAccRecordUsingPOST,
  accRecordCheckPurUsingPOST,
  accRecordUnCheckPurUsingPOST,
} from '@/api/api/qiantaizhangwu.ts'
export default {
  name: 'CashierPaymentRecordDetail',
  components: {},
  data() {
    return {
      info: {},
      advanced: false,
      priceMdll: null,
      editMdll: null,
      priceVisible: false,
      editVisible: false,
      priceLoading: false,
      editLoading: false,
      searchLoading: false,
      recordLoading: false,
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        queryCondition: '',
        status: '',
      },
      pagination: {},
      columns: [
        {
          title: '账户',
          dataIndex: 'accountName',
        },
        {
          title: '金额',
          dataIndex: 'payValue',
        },
        {
          title: '支付方式',
          dataIndex: 'paymentMethodName',
        },
        {
          title: '备注',
          dataIndex: 'memo',
        },
      ],
      loadData: [],
      recordcolumns: [
        {
          title: '时间',
          dataIndex: 'operTime',
        },
        {
          title: '类型',
          dataIndex: 'action',
        },
        {
          title: '操作',
          dataIndex: 'option',
          width: '150px',
          scopedSlots: { customRender: 'option' },
        },
      ],
      recordloadData: [],
      recordPagination: {},
    }
  },
  mounted() {
    this.getDetail()
    this.handleSearch(this.queryParam)
    this.handleRecordSearch(this.queryParam)
  },
  methods: {
    handleUserDetail(record) {
      this.$router.push({ path: `/user/user/${record.operator}` })
    },
    render(type) {
      switch (type) {
        case '0':
          return '新增收入'
        case '1':
          return '新增支出'
        case '2':
          return '收预付款'
        case '3':
          return '退预付款'
       
      }
    },
    getDetail() {
      getAccRecordUsingGET({ id: this.$route.params.id })
        .then((res) => {
          this.info = res.data
        })
        .catch(() => {
          this.$message.error('获取详情失败,请联系管理员')
        })
    },
    handleSearch(param) {
      this.searchLoading = true
      getSalPayUsingGET({ ...param,  orderId: this.$route.params.id })
        .then((res) => {
          this.searchLoading = false
          this.loadData = res.data.list
          this.queryParam.pageNo = this.pagination.current
          this.pagination = { ...this.pagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.searchLoading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
 pager.pageSize = pagination.pageSize
      this.pagination = pager
      this.handleSearch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters,
        pageNo: pagination.current,
        pageSize: pagination.pageSize,
        queryCondition: this.queryParam.queryCondition,
        status: this.queryParam.status,
        date: this.queryParam.date,
      })
    },
    handleRecordSearch(params) {
      this.recordLoading = true
      getOperRecordListUsingPOST({ ...params, orderId: this.$route.params.id })
        .then((res) => {
          this.recordloadData = res.data.list
          this.recordLoading = false
          this.recordPagination = { ...this.recordPagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.recordLoading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleRecordTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
 pager.pageSize = pagination.pageSize
      this.recordPagination = pager
      this.handleRecordSearch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters,
        pageNo: pagination.current,
        pageSize: pagination.pageSize,
        queryCondition: '',
        status: '',
      })
    },
    handleCheckPur() {
      const that = this
      that.$confirm({
        title: '审核通过',
        content: '你确定要审核通过该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          accRecordCheckPurUsingPOST({ id: that.$route.params.id })
            .then(() => {
              that.handleSearch(that.queryParam)
              that.handleRecordSearch(that.queryParam)
              that.getDetail()
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    handleOrder(info) {
      this.$router.push({ path: `/cashier/createFinanceIncomeShop/create/${info.storeId ?? 1}` })
    },
    handleUnCheckPur() {
      const that = this
      that.$confirm({
        title: '反审核',
        content: '你确定要反审核该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          accRecordUnCheckPurUsingPOST({ id: that.$route.params.id })
            .then(() => {
              that.handleSearch(that.queryParam)
              that.handleRecordSearch(that.queryParam)
              that.getDetail()
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    handleBack(info) {
      const that = this
      that.$confirm({
        title: '撤销订单',
        content: '你确定要撤销该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          revokeAccRecordUsingPOST({ id: info.id })
            .then(() => {
              that.$router.back()
              that.$message.success('操作成功')
            })
            .catch((error) => {
              console.log(error)
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
  },
}
</script>
